package com.luyuan.crm.sale.web;

import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.product.model.Product;
import com.luyuan.crm.product.model.ProductType;
import com.luyuan.crm.sale.model.OrderProduct;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.web.jsf.action.BaseAction;

@Component("addOrderProductAction")
@Scope("view")
public class AddOrderProductAction extends BaseAction implements InitializingBean {
    private List<OrderProduct> products;

    private OrderProduct orderProduct = new OrderProduct();

    private Product product;

    private ProductType productType;

    public void addOrderProduct() {
        orderProduct.setPrice(product.getPrice());
        orderProduct.setProduct(product);
        orderProduct.setName(product.getName());
        orderProduct.setSpecification(product.getSpecification());
        orderProduct.setSubTotal(orderProduct.getPrice() * orderProduct.getCount());

        if (products == null) {
            addGlobalI18nMessage(new BusinessException("新增购买产品出错，请关闭“添加产品信息”对话框后重新偿试！"));
        }
        else {
            products.add(orderProduct);
        }
        orderProduct = new OrderProduct();
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public OrderProduct getOrderProduct() {
        return orderProduct;
    }

    public void setOrderProduct(OrderProduct orderProduct) {
        this.orderProduct = orderProduct;
    }

    public ProductType getProductType() {
        return productType;
    }

    public void setProductType(ProductType productType) {
        this.productType = productType;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void afterPropertiesSet() throws Exception {
        products = (List<OrderProduct>) getRequestAttribute("products");
    }
}
